package com.liyiyan.note;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.liyiyan.note.R;
import com.liyiyan.note.adapter.DisplayAdapter;
import com.liyiyan.note.bean.NoteBean;
import com.liyiyan.note.util.GetHtmlService;

@SuppressLint({ "HandlerLeak", "ParserError" })
public class NoteDisplayActivity extends Activity {
	protected static final String TAG = "NoteDisplayActivity";
	private int userID = 1; // for testing purpose
	private List<NoteBean> notes_list = new ArrayList<NoteBean>();
	private DisplayAdapter dp;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.display);
		ListView listView = (ListView) findViewById(R.id.displayListView);
		listView.setEmptyView(findViewById(R.id.emptyNoteTextView));
		System.out.println(dp);
		if (dp==null) {
			String userIdString = "" + userID;
			List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
			nameValuePairs.add(new BasicNameValuePair("pid", userIdString));
			nameValuePairs.add(new BasicNameValuePair("type", "" + 1));
			new GetHtmlService(NoteDisplayActivity.this, handler,
					GetHtmlService.DISPLAY, nameValuePairs).start();
		} 
		
	}
	

	private final Handler delete_handler = new Handler(Looper.getMainLooper()) {
		public void handleMessage(Message msg) {
			String contentString = msg.getData().getString("result");
			Log.i(TAG, contentString);

		}
	};

	private final Handler handler = new Handler(Looper.getMainLooper()) {

		public void handleMessage(Message msg) {
			String contentString = msg.getData().getString("result");
			JSONArray jsonArray;
			try {
				jsonArray = new JSONArray(contentString);
				@SuppressWarnings("unused")
				Map<String, String> map;
				NoteBean noteBean;

				for (int i = 0; i < jsonArray.length(); i++) {
					// {"_id":"17","msg_date":"2012-05-20","msg":null,"msg_title":"this is title"}
					noteBean = new NoteBean();
					map = new HashMap<String, String>();
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					String _id = jsonObject.getString("_id");
					String msg_date = jsonObject.getString("msg_date");
					String message = jsonObject.getString("msg");
					String msg_title = jsonObject.getString("msg_title");
					noteBean.set_id(Integer.parseInt(_id));
					noteBean.setContentString(message);
					noteBean.setLastModifiedString(msg_date);
					noteBean.setTitleString(msg_title);
					notes_list.add(noteBean);

				}

				ListView notes = (ListView) findViewById(R.id.displayListView);

				dp = new DisplayAdapter(NoteDisplayActivity.this, notes_list);
				notes.setAdapter(dp);

				notes.setOnItemLongClickListener(new OnItemLongClickListener() {

					public boolean onItemLongClick(AdapterView<?> arg0,
							View arg1, int arg2, long arg3) {
						// TODO Auto-generated method stub
						final int position = arg2;
						final long _id = arg3;
						String deleteTitleString = "Delete?";
						String deleteMessageString = "Are you sure you want to delete this note?";
						String deleteYesString = "Yes";
						String deleteNoString = "Cancel";
						new AlertDialog.Builder(NoteDisplayActivity.this)
								.setTitle(deleteTitleString)
								.setMessage(deleteMessageString)
								.setPositiveButton(deleteYesString,
										new DialogInterface.OnClickListener() {
											public void onClick(
													DialogInterface dialog,
													int which) {
												// NoteDisplayActivity.this.notes_list.remove(position);
												dp.pop(position); // remove from
																	// listview
												List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
														1);
												nameValuePairs
														.add(new BasicNameValuePair(
																"id", "" + _id));
												new GetHtmlService(
														NoteDisplayActivity.this,
														delete_handler,
														GetHtmlService.DELETE,
														nameValuePairs).start();

												return;
											}
										})
								.setNegativeButton(deleteNoString, null).show();
						return false;
					}
				});

			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	};

}